package com.zxcrm.zxcrm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.zxcrm.common.utils.DateUtils;
import com.zxcrm.common.utils.SecurityUtils;
import com.zxcrm.zxcrm.domain.ZxcrmScore;
import com.zxcrm.zxcrm.domain.ZxcrmScoreEvaluate;
import com.zxcrm.zxcrm.mapper.ZxcrmScoreMapper;
import com.zxcrm.zxcrm.service.IZxcrmScoreEvaluateService;
import com.zxcrm.zxcrm.service.IZxcrmScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评分表Service业务层处理
 * 
 * @author xu
 * @date 2025-07-14
 */
@Service
public class ZxcrmScoreServiceImpl implements IZxcrmScoreService 
{
    @Autowired
    private ZxcrmScoreMapper zxcrmScoreMapper;
    @Autowired
    private IZxcrmScoreEvaluateService zxcrmScoreEvaluateService;

    /**
     * 查询评分表
     * 
     * @param id 评分表主键
     * @return 评分表
     */
    @Override
    public ZxcrmScore selectZxcrmScoreById(Long id)
    {
        return zxcrmScoreMapper.selectZxcrmScoreById(id);
    }

    /**
     * 查询评分表列表
     * 
     * @param zxcrmScore 评分表
     * @return 评分表
     */
    @Override
    public List<ZxcrmScore> selectZxcrmScoreList(ZxcrmScore zxcrmScore)
    {
        return zxcrmScoreMapper.selectZxcrmScoreList(zxcrmScore);
    }

    /**
     * 新增评分表
     * 
     * @param zxcrmScore 评分表
     * @return 结果
     */
    @Override
    public int insertZxcrmScore(ZxcrmScore zxcrmScore)
    {
        zxcrmScore.setCreateTime(DateUtils.getNowDate());
        return zxcrmScoreMapper.insertZxcrmScore(zxcrmScore);
    }

    /**
     * 修改评分表
     * 
     * @param zxcrmScore 评分表
     * @return 结果
     */
    @Override
    public int updateZxcrmScore(ZxcrmScore zxcrmScore)
    {
        zxcrmScore.setUpdateTime(DateUtils.getNowDate());
        return zxcrmScoreMapper.updateZxcrmScore(zxcrmScore);
    }

    /**
     * 批量删除评分表
     * 
     * @param ids 需要删除的评分表主键
     * @return 结果
     */
    @Override
    public int deleteZxcrmScoreByIds(Long[] ids)
    {
        return zxcrmScoreMapper.deleteZxcrmScoreByIds(ids);
    }

    /**
     * 删除评分表信息
     * 
     * @param id 评分表主键
     * @return 结果
     */
    @Override
    public int deleteZxcrmScoreById(Long id)
    {
        return zxcrmScoreMapper.deleteZxcrmScoreById(id);
    }

    @Override
    public List<ZxcrmScore> selectZxcrmScoreListUserScore(ZxcrmScore zxcrmScore)
    {
        return zxcrmScoreMapper.selectZxcrmScoreListUserScore(zxcrmScore);
    }

    @Override
    public int score(ZxcrmScore zxcrmScore) {
        zxcrmScore.setScoreStatus("1");
        zxcrmScore.setLastScoreTime(new Date());
        zxcrmScore.setLastScoreUserId(SecurityUtils.getUserId());
        zxcrmScore.setCreateTime(DateUtils.getNowDate());
        zxcrmScore.setCreateBy(SecurityUtils.getUserId().toString());
        zxcrmScoreMapper.insertZxcrmScore(zxcrmScore);
        setScoreEvaluate(zxcrmScore);
        return 1;
    }

    @Override
    public List<ZxcrmScore> listUserScoreByUserId(Long userId)
    {
        return zxcrmScoreMapper.listUserScoreByUserId(userId);
    }

    @Override
    public List<ZxcrmScore> scoreStatisticsDetailList(ZxcrmScore zxcrmScore) {
        return zxcrmScoreMapper.scoreStatisticsDetailList(zxcrmScore);
    }

    private void setScoreEvaluate(ZxcrmScore zxcrmScore)
    {
        if (ObjectUtil.isNotEmpty(zxcrmScore.getEvaluateIds()))
        {
            List<ZxcrmScoreEvaluate> zxcrmScoreEvaluateList = Arrays.stream(zxcrmScore.getEvaluateIds().split(",")).map(x -> new ZxcrmScoreEvaluate(zxcrmScore.getId(), Long.parseLong(x))).collect(Collectors.toList());
            zxcrmScoreEvaluateService.insertBatch(zxcrmScoreEvaluateList);
        }

    }
}
